package com.such.kit.image;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import com.such.kit.file.FileCommons;
import com.such.kit.validate.ValidateCommons;

/**
 * @作者 SUCH
 * @日期 2017-3-1 上午10:06:03
 * @描述 图片备注（水印）工具类
 */
public class ImageRemark {

	public static final String POSITION_TOP_LEFT = "positionTopLeft";
	public static final String POSITION_TOP_RIGHT = "positionTopRight";
	public static final String POSITION_BOTTOM_LEFT = "positionBottomLeft";
	public static final String POSITION_BOTTOM_RIGHT = "positionBottomRight";
	public static final String POSITION_TOP_MIDDLE = "positionTopMiddle";
	public static final String POSITION_BOTTOM_MIDDLE = "positionBottomMiddle";
	public static final String POSITION_LEFT_MIDDLE = "positionLeftMiddle";
	public static final String POSITION_RIGHT_MIDDLE = "positionRightMiddle";
	public static final String POSITION_MIDDLE = "positionMiddle";

	private String enlargeName;
	private boolean usePrefix;
	private String prefixName;
	private boolean useSuffix;
	private String suffixName;

	/**
	 * <p>方法名：setEnlargeName</p>
	 * <p>描述：设置扩展名，默认使用原扩展名</p>
	 * @param enlargeName 扩展名
	 */
	public void setEnlargeName(String enlargeName) {
		this.enlargeName = enlargeName;
	}

	/**
	 * <p>方法名：usePrefix</p>
	 * <p>描述：为图片设置前缀</p>
	 * @param fixName 缀名
	 */
	public void usePrefix(String fixName) {
		if (ValidateCommons.isNotEmpty(fixName)) {
			this.usePrefix = true;
			this.prefixName = fixName;
		}
	}

	/**
	 * <p>方法名：useSuffix</p>
	 * <p>描述：为图片设置后缀</p>
	 * @param fixName 缀名
	 */
	public void useSuffix(String fixName) {
		if (ValidateCommons.isNotEmpty(fixName)) {
			this.useSuffix = true;
			this.suffixName = fixName;
		}
	}

	/**
	 * <p>方法名：imageRemark</p>
	 * <p>描述：使用图片备注</p>
	 * @param targetImage 目标图片
	 * @param remarkText 备注图片
	 * @param position 备注位置
	 * @param x 横坐标修正
	 * @param y 纵坐标修正
	 * @throws IOException
	 */
	public void imageRemark(String targetImage, String remarkImage, String position, int x, int y, float alpha) 
			throws IOException {
		try {
			File imageFile = new File(targetImage);
			BufferedImage bufImage = ImageIO.read(imageFile);
			int width = bufImage.getWidth();
			int height = bufImage.getHeight();
			Graphics2D g2D = bufImage.createGraphics();
			g2D.drawImage(bufImage, 0, 0, width, height, null);
			File tempImage = new File(remarkImage);
			BufferedImage tempBufImage = ImageIO.read(tempImage);
			int tempWidth = tempBufImage.getWidth();
			int tempHeight = tempBufImage.getHeight();
			AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha);
			g2D.setComposite(ac);
			int xValue = positionWidth(width, tempWidth, x, position);
			int yValue = positionHeight(height, tempHeight, y, position);
			g2D.drawImage(tempBufImage, xValue, yValue, tempWidth, tempHeight, null);
			g2D.dispose();
			String enlargeName = FileCommons.getEnlargeName(targetImage);
			if (ValidateCommons.isNotEmpty(this.enlargeName)) {
				enlargeName = this.enlargeName;
			}
			if (this.usePrefix) {
				targetImage = FileCommons.addPerfix(targetImage, this.prefixName);
				imageFile = new File(targetImage);
			}
			if (this.useSuffix) {
				targetImage = FileCommons.addSuffix(targetImage, this.suffixName);
				imageFile = new File(targetImage);
			}
			ImageIO.write(bufImage, enlargeName, imageFile);
		} catch (IOException e) {
			throw e;
		}
	}

	/**
	 * <p>方法名：textRemark</p>
	 * <p>描述：使用文字备注</p>
	 * @param targetImage 目标图片
	 * @param remarkText 备注文字
	 * @param font 字体
	 * @param color 颜色
	 * @param position 备注位置
	 * @param x 横坐标修正
	 * @param y 纵坐标修正
	 * @param alpha 透明度
	 * @throws IOException
	 */
	public void textRemark(String targetImage, String remarkText, Font font, Color color, String position, int x, int y, float alpha) 
			throws IOException {
		try {
			File imageFile = new File(targetImage);
			BufferedImage bufImage = ImageIO.read(imageFile);
			int width = bufImage.getWidth();
			int height = bufImage.getHeight();
			Graphics2D g2D = bufImage.createGraphics();
			g2D.drawImage(bufImage, 0, 0, width, height, null);
			g2D.setFont(font);
			g2D.setColor(color);
			AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha);
			g2D.setComposite(ac);
			int fontSize = font.getSize();
			int xValue = positionWidth(width, (getTextLength(remarkText) * fontSize), x, position);
			int yValue = positionHeight(height, fontSize, y, position);
			g2D.drawString(remarkText, xValue, yValue);
			g2D.dispose();
			String enlargeName = FileCommons.getEnlargeName(targetImage);
			if (ValidateCommons.isNotEmpty(this.enlargeName)) {
				enlargeName = this.enlargeName;
			}
			if (this.usePrefix) {
				targetImage = FileCommons.addPerfix(targetImage, this.prefixName);
				imageFile = new File(targetImage);
			}
			if (this.useSuffix) {
				targetImage = FileCommons.addSuffix(targetImage, this.suffixName);
				imageFile = new File(targetImage);
			}
			ImageIO.write(bufImage, enlargeName, imageFile);
		} catch (IOException e) {
			throw e;
		}
	}

	private int positionWidth(int targetWidth, int remarkWidth, int x, String position) {
		int xValue = 0;
		if (POSITION_TOP_LEFT.equals(position)) {
			xValue = x;
		} else if (POSITION_TOP_RIGHT.equals(position)) {
			xValue = targetWidth - remarkWidth + x;
		} else if (POSITION_BOTTOM_LEFT.equals(position)) {
			xValue = x;
		} else if (POSITION_BOTTOM_RIGHT.equals(position)) {
			xValue = targetWidth - remarkWidth + x;
		} else if (POSITION_TOP_MIDDLE.equals(position)) {
			xValue = (targetWidth - remarkWidth) / 2 + x;
		} else if (POSITION_BOTTOM_MIDDLE.equals(position)) {
			xValue = (targetWidth - remarkWidth) / 2 + x;
		} else if (POSITION_LEFT_MIDDLE.equals(position)) {
			xValue = x;
		} else if (POSITION_RIGHT_MIDDLE.equals(position)) {
			xValue = targetWidth - remarkWidth + x;
		} else {
			xValue = (targetWidth - remarkWidth) / 2 + x;
		}
		return xValue;
	}

	private int positionHeight(int targetHeight, int remarkHeight, int y, String position) {
		int yValue = 0;
		if (POSITION_TOP_LEFT.equals(position)) {
			yValue = y;
		} else if (POSITION_TOP_RIGHT.equals(position)) {
			yValue = y;
		} else if (POSITION_BOTTOM_LEFT.equals(position)) {
			yValue = targetHeight - remarkHeight + y;
		} else if (POSITION_BOTTOM_RIGHT.equals(position)) {
			yValue = targetHeight - remarkHeight + y;
		} else if (POSITION_TOP_MIDDLE.equals(position)) {
			yValue = y;
		} else if (POSITION_BOTTOM_MIDDLE.equals(position)) {
			yValue = targetHeight - remarkHeight + y;
		} else if (POSITION_LEFT_MIDDLE.equals(position)) {
			yValue = (targetHeight - remarkHeight) / 2 + y;
		} else if (POSITION_RIGHT_MIDDLE.equals(position)) {
			yValue = (targetHeight - remarkHeight) / 2 + y;
		} else {
			yValue = (targetHeight - remarkHeight) / 2 + y;
		}
		return yValue;
	}

	private int getTextLength(String text) {
		int length = 0;
		for (int i = 0; i < text.length(); i++) {
			char c = text.charAt(i);
			String str = String.valueOf(c);
			if (str.getBytes().length > 1) {
				length += 2;
			} else {
				length += 1;
			}
		}
		length = length / 2;
		if (length > 0) {
			length++;
		}
		return length;
	}

}
